package com.mingtu.guards.base;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import static com.mingtu.guards.utils.MyConstant.isOpenLoadAnimation;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2023-3-9 16:43:36
 * @Email : 1281573890@qq.com
 * @Eescription :
 */
public class BaseAnimatorAdapter<ViewHolder extends RecyclerView.ViewHolder, ListBean> extends RecyclerView.Adapter<ViewHolder> implements View.OnClickListener,View.OnLongClickListener {

    public List<ListBean> mData = new ArrayList<>();
    public Context context;
    public int type;
    public boolean showAnimator = isOpenLoadAnimation;
    private OnItemClickListener mOnItemClickListener = null;
    private OnItemLongClickListener mOnItemLongClickListener = null;
    public BaseAnimatorAdapter(Context context) {
        this.context = context;
    }

    public BaseAnimatorAdapter(Context context, int type) {
        this.context = context;
        this.type = type;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        addAnimation(holder);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemLongClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemLongClickListener.onItemLongClick(v, (int) v.getTag());
        }
        return false;
    }

    public void setShowAnimator(boolean showAnimator) {
        this.showAnimator = showAnimator;
    }

    private void addAnimation(ViewHolder holder) {
        if (showAnimator) {
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.itemView, View.SCALE_X, 0.6f, 1.0f);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(holder.itemView, View.SCALE_Y, 0.6f, 1.0f);
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.setInterpolator(new DecelerateInterpolator());
            animatorSet.setDuration(300);
            animatorSet.playTogether(scaleX, scaleY);
            animatorSet.start();
        }
    }


    public void addData(List<ListBean> mNewData) {
        mData.addAll(mNewData);
        notifyItemRangeInserted(mData.size() - mNewData.size(), mNewData.size());
    }

    public void addData(ListBean listBean) {
        mData.add(listBean);
        notifyItemRangeInserted(mData.size() , 1);
    }

    public void replaceData(List<ListBean> data) {
        clearData();
        addData(data);
    }


    public void clearData() {
        notifyItemRangeRemoved(0, mData.size());
        mData.clear();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.mOnItemLongClickListener = listener;
    }

    public static interface OnItemLongClickListener {
        void onItemLongClick(View view, int position);
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public List<ListBean> getData() {
        return mData;
    }
}
